package com.leetcode.www;
import java.util.*;
/**
 * Creared with IntelliJ IDEA.
 * Description:给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多，同时不在禁用列表中的单词。
 *
 * 题目保证至少有一个词不在禁用列表中，而且答案唯一。
 *
 * 禁用列表中的单词用小写字母表示，不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/most-common-word
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * User:yxd
 * Date:2022-04-17
 * Time:21:07
 */
public class Everyday819 {
    public String mostCommonWord(String paragraph, String[] banned) {
        Set<String> bannedSet = new HashSet<String>();
        for (String word : banned) {
            bannedSet.add(word);
        }
        int maxFrequency = 0;
        Map<String, Integer> frequencies = new HashMap<String, Integer>();
        StringBuffer sb = new StringBuffer();
        int length = paragraph.length();
        for (int i = 0; i <= length; i++) {
            if (i < length && Character.isLetter(paragraph.charAt(i))) {
                sb.append(Character.toLowerCase(paragraph.charAt(i)));
            } else if (sb.length() > 0) {
                String word = sb.toString();
                if (!bannedSet.contains(word)) {
                    int frequency = frequencies.getOrDefault(word, 0) + 1;
                    frequencies.put(word, frequency);
                    maxFrequency = Math.max(maxFrequency, frequency);
                }
                sb.setLength(0);
            }
        }
        String mostCommon = "";
        Set<Map.Entry<String, Integer>> entries = frequencies.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String word = entry.getKey();
            int frequency = entry.getValue();
            if (frequency == maxFrequency) {
                mostCommon = word;
                break;
            }
        }
        return mostCommon;
    }

    public String mostCommonWord1(String paragraph, String[] banned) {
        Map<String,Integer> map = new HashMap<>();
        int i = 0;
        Set<String> set = new HashSet<>();
        for(String s : banned){
            set.add(s);
        }
        int j = 0;
        int count = 0;
        String s = "";
        String paragraph1 = paragraph.toLowerCase();

        while(i < paragraph1.length()){
            j = i;
            while(i < paragraph1.length() && Character.isLetter(paragraph1.charAt(i))){
                i ++;
            }
            if(!map.containsKey(paragraph1.substring(j,i))){
                map.put(paragraph1.substring(j,i),1);
            }else{
                map.put(paragraph1.substring(j,i),map.get(paragraph1.substring(j,i)) + 1);
            }
            if(count < map.get(paragraph1.substring(j,i)) && !set.contains(paragraph1.substring(j,i))){
                count = map.get(paragraph1.substring(j,i));
                s = paragraph1.substring(j,i);
            }
            while(i < paragraph1.length() && !Character.isLetter(paragraph1.charAt(i))){
                i ++;
            }

        }
        return s;

    }
}
